package com.example.idea.service;

import com.example.idea.entity.Dao.UserDao;
import com.example.idea.entity.User;
import com.example.idea.exception.ServiceException;
import com.example.idea.mapper.UserMapper;
import com.example.idea.utils.Page;
import com.example.idea.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;

    public void add(User user) {
        user.setPassword("888");
        userMapper.add(user);
    }

    public void update(User user) {
        userMapper.update(user);
    }

    public void delete(Integer id) {
        userMapper.delete(id);
    }

    public User query(Integer id) {
        return userMapper.query(id);
    }

    public void deleteBatch(List<Integer> ids) {
        userMapper.deleteBatch(ids);
    }

    public List<User> list() {
        return userMapper.list();
    }

    public List<User> selectList(User user) {
        return userMapper.selectList(user);
    }

    public List<User> page(User user) {
        return userMapper.selectListPage(user);
    }

    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<User> page = new Page<>();
        List<User> userList = userMapper.selectByPage(skipNum, pageSize, username, name);
        Integer total = userMapper.selectCountByPage(username, name);
        page.setList(userList);
        page.setTotal(total);
        return page;
    }

    /**
     * 登录
     * @return
     */
    public User login(User user) {
        //根据用户名查询数据库的用户信息
        User dbUser = userMapper.selectByUserName(user.getUsername());
        if (dbUser == null) {
            throw new ServiceException("用户名或密码错误!");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new ServiceException("用户名或密码错误!");
        }
        //        生成Token
        String token = TokenUtils.createToken(dbUser.getId(), dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

//    注册
    public User addPlusUser(User user) {
        // 检查用户是否已存在
        User dbUser = userMapper.selectByUser(user.getUsername());
        if (dbUser != null) {
            throw new ServiceException("用户已存在，请选择其他用户名！");
        } else {
            // 插入用户信息
            user.setAddress("中国");
            user.setEmail("338677@557");
            user.setPhone("123456789");
            int rowsAffected = userMapper.addPlusUser(user);
            if (rowsAffected > 0) {
                // 插入成功后，可以通过主键或其他唯一标识查询插入的用户信息
                // 假设插入后可以立即通过用户名查询到新用户
                return userMapper.selectByUser(user.getUsername());
            } else {
                throw new ServiceException("用户添加失败！");
            }
        }
    }

    public User getById(Integer valueOf) {
        return userMapper.query(valueOf);
    }

    public void forgetUsernameAndPassword(UserDao user) {
        UserDao userDao = userMapper.selectByUserDao(user.getUsername());
        if (userDao == null) {
            throw new ServiceException("用户名错误!");
        }
        if (!user.getPhone().equals(userDao.getPhone())) {
            throw new ServiceException("手机号错误!");
        }
        userDao.setPassword("666");
        userMapper.updateDao(userDao);
    }

    public List<User> selectListAll(String username, String name) {
        return userMapper.selectListAll(username, name);
    }

    public List<User> selectByIds(List<Integer> id) {
        return userMapper.selectByIds(id);
    }

    public void addInfo(User userList) {
        userMapper.addInfo(userList);
    }
}
